﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Globalization;
using Facebook.Entity;

/// <summary>
/// Summary description for WorkItem
/// </summary>
public class WorkItem
{
    private int _id;
    private string _title;
    private string _description;
    private string _url;
    private bool _hasImage;
    private int _month;
    private int _year;

    public int ID
    {
        get
        {
            return this._id;
        }

        set
        {
            this._id = value;
        }
    }

    public string Title
    {
        get
        {
            return this._title;
        }

        set
        {
            this._title = value;
        }
    }

    public string Description
    {
        get
        {
            return this._description;
        }

        set
        {
            this._description = value;
        }
    }

    public string Url
    {
        get
        {
            return this._url;
        }

        set
        {
            this._url = value;
        }
    }

    public bool HasImage
    {
        get
        {
            return this._hasImage;
        }

        set
        {
            this._hasImage = value;
        }
    }


    public int Month
    {
        get
        {
            return this._month;
        }
        
        set
        {
            this._month= value;
        }
    }

    public int Year
    {
        get
        {
            return this._year;
        }

        set
        {
            this._year = value;
        }
    }

    public string CreationDate
    {
        get
        {
            return string.Format("{0} {1}", DateTimeFormatInfo.CurrentInfo.GetMonthName(_month), _year);
        }
    }

    public ReadOnlyCollection<FacebookUser> UsersParticipated
    {
        get
        {
            return FacebookUser.GetFacebookUsers(_id);
        }
    }

    private void FillMe(WorkItem ach)
    {
        //this._description = ach.Description;
        //this._hasImage = ach.HasImage;
        //this._title = ach.Title;
        //this._url = ach.Url;
        //this._month = ach._hasImage;
    }

    public static void AddNew(string userID, string title, string description, string url, DateTime date, bool hasImage)
    {
        DbConnection.ExecuteNonQuery(string.Format("INSERT INTO WorkItem(USER_ID, TITLE, DESCRIPTION, URL, CREATIONDATE, HASIMAGE) VALUES ('{0}','{1}','{2}','{3}', '{4}', {5})", userID, title, description, url, date, Convert.ToInt16(hasImage)));
    }
        
    public static ReadOnlyCollection<WorkItem> GetWorkItems(params string[] userID)
    {
        string sql = string.Format("SELECT * FROM WorkItem WHERE user_id = '{0}'", userID[0]);

        for (int i = 1; i < userID.Length; i++)
        {
            sql = sql + " OR user_id = '" + userID[i] + "'";
        }

        SqlDataReader reader = DbConnection.ExecuteSelect(sql);        

        return GetBusinessObjects(reader);
    }


    public static WorkItem GetWorkItem(string userID, string title)
    {
        SqlDataReader reader = DbConnection.ExecuteSelect(string.Format("SELECT * FROM WorkItem WHERE USER_ID={0} AND TITLE='{1}'", userID, title));

        ReadOnlyCollection<WorkItem> items = WorkItem.GetBusinessObjects(reader);

        if (items.Count > 0)
        {
            return items[0];
        }

        return null;
    }

    public static WorkItem GetWorkItem(int id)
    {
        SqlDataReader reader = DbConnection.ExecuteSelect(string.Format("SELECT * FROM WorkItem WHERE ID={0}", id));

        ReadOnlyCollection<WorkItem> items = WorkItem.GetBusinessObjects(reader);
                
        if (items.Count > 0)
        {
            return items[0];
        }

        return null;
    }

    private static ReadOnlyCollection<WorkItem> GetBusinessObjects(SqlDataReader reader)
    {
        List<WorkItem> _list = new List<WorkItem>();

        while (reader.Read())
        {
            WorkItem WorkItem = new WorkItem();

            WorkItem.ID = int.Parse(reader["ID"].ToString());
            WorkItem.Title = reader["Title"].ToString();
            WorkItem.Description = reader["Description"].ToString();
            WorkItem.Url = reader["Url"].ToString();
            WorkItem.HasImage = Convert.ToBoolean(reader["HasImage"]);

            DateTime tmp = Convert.ToDateTime(reader["CreationDate"].ToString());
            WorkItem.Month = tmp.Month;
            WorkItem.Year = tmp.Year;

            _list.Add(WorkItem);
        }

        return _list.AsReadOnly();
    }

}
